//
//  IdentitySViewController.m
//  ScalperMerchant
//
//  Created by 王云 on 2017/10/11.
//  Copyright © 2017年 WY. All rights reserved.
//

#import "IdentitySViewController.h"
#import "HomeController.h"
#import "BusinessAgreeController.h"
#import "SCScanController.h"
#import  <AVFoundation/AVCaptureDevice.h>

@interface IdentitySViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,weak)UITableView *tableV;
@property (nonatomic,strong)NSArray *data;
@property (nonatomic,assign)NSInteger seleced;

@property (nonatomic,weak)UIView *footV;
@property (nonatomic,weak)UITextField *codeF;

@property (nonatomic,weak)UIButton *yesBtn;

@end

@implementation IdentitySViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"身份选择";
    [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
    }];
    
    [self setUI];
    [self initData];
}
- (void)initData{
    
    _seleced = 1;
    _data = @[@{@"type":@"我是店主",@"val":@(1)},
              @{@"type":@"我是店员",@"val":@(2)}];
    [_tableV reloadData];
}

- (void)setUI{
    
    [self.view addSubview:self.tableV];
    
    
    UIView *footV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 40)];
    footV.backgroundColor = [UIColor whiteColor];
    footV.hidden = YES;
//    self.tableV.tableFooterView = footV;
    [self.view addSubview:footV];
    _footV = footV;
    
    UILabel *codeL = [[UILabel alloc] init];
    codeL.text = @"  入驻码";
    codeL.font = [UIFont systemFontOfSize:14];
    [footV addSubview:codeL];
    
    UIButton *codeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [codeBtn setTitleColor:TitleDarkColor forState:UIControlStateNormal];
    [codeBtn addTarget:self action:@selector(codeClick) forControlEvents:UIControlEventTouchUpInside];
    [codeBtn setTitle:@"扫码入驻" forState:UIControlStateNormal];
    codeBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [footV addSubview:codeBtn];
    
    UIImageView *line = [[UIImageView alloc] init];
    line.backgroundColor = TitleDarkColor;
    [footV addSubview:line];
    
    UITextField *codeF = [[UITextField alloc] init];
    codeF.font = [UIFont systemFontOfSize:14];
    codeF.placeholder = @"输入入驻码";
    [footV addSubview:codeF];
    _codeF = codeF;
    
    UIButton *yesBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [yesBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [yesBtn addTarget:self action:@selector(yesClick) forControlEvents:UIControlEventTouchUpInside];
    yesBtn.layer.cornerRadius = 3;
    [yesBtn setTitle:@"确定" forState:UIControlStateNormal];
    yesBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [yesBtn setBackgroundColor:KOrange];
    [self.view addSubview:yesBtn];
    _yesBtn = yesBtn;
    
    __weak __typeof(&*self)weakSelf = self;
    [self.tableV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.view);
        make.top.equalTo(weakSelf.view.mas_top).offset(12);
        make.height.equalTo(@88);
    }];
    [footV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.tableV.mas_bottom).offset(12);
        make.left.right.equalTo(weakSelf.view);
        make.size.mas_equalTo(footV.frame.size);
    }];
    [codeL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(footV);
        make.left.equalTo(footV.mas_left).offset(8);
        make.size.mas_equalTo(CGSizeMake(80, 30));
    }];
    [codeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(footV);
        make.right.equalTo(footV.mas_right).offset(-8);
        make.size.mas_equalTo(CGSizeMake(80, 30));
    }];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(footV);
        make.right.equalTo(codeBtn.mas_left).offset(-4);
        make.size.mas_equalTo(CGSizeMake(1, 28));
    }];
    [codeF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(footV);
        make.left.equalTo(codeL.mas_right).offset(8);
        make.right.equalTo(line.mas_right).offset(-8);
    }];
    [yesBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(footV.mas_bottom).offset(22);
        make.left.equalTo(weakSelf.view.mas_left).offset(8);
        make.right.equalTo(weakSelf.view.mas_right).offset(-8);
        make.height.equalTo(@38);
    }];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.data.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"ZYpaycellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.font = [UIFont systemFontOfSize:14];
    }
    NSDictionary *item = _data[indexPath.row];
    cell.textLabel.text = item[@"type"];
    
    if ([item[@"val"] integerValue] == _seleced) {
        cell.accessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ID_circular_region_sel"]];
    }else{
        cell.accessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ID_circular_region"]];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *item = _data[indexPath.row];
    self.seleced = [item[@"val"] integerValue];
    [self.tableV reloadData];
}
- (void)setSeleced:(NSInteger)seleced{
    _seleced = seleced;
    if (_seleced == 2) {
        _footV.hidden = NO;
    }else{
        _footV.hidden = YES;
    }
}

- (UITableView *)tableV{
    if (_tableV == nil) {
        UITableView *tableV = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
        tableV.scrollEnabled = NO;
        tableV.showsVerticalScrollIndicator = NO;
        tableV.backgroundColor = DarkColor;
        [tableV setSeparatorColor:DarkColor];
        tableV.dataSource = self;
        tableV.delegate = self;
        tableV.tableFooterView = [UIView new];
        [self.view addSubview:tableV];
        _tableV = tableV;
    }
    return _tableV;
}

- (void)yesClick{
    
    [self.view endEditing:YES];
    
    
    SCUser *user = [SCUserTool loadInfo];
    if (_seleced == 1) {
        
        BusinessAgreeController *agreeVC = [[BusinessAgreeController alloc] init];
       agreeVC.token = [NSString stringWithFormat:@"%@",user.token];
        [self.navigationController pushViewController:agreeVC animated:YES];
        
    }else{
        if (_codeF.text.length == 0) {
            [SCTool showError:@"请输入正确授权码"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [_codeF becomeFirstResponder];
            });
            return;
        }

        [SCTool showLoading:@"正在请求"];
        __weak __typeof(&*self)weakSelf = self;
        [SCApiTool ScauthcodetomemberAuth_code:_codeF.text success:^(NSString *msg) {
            
            [SCTool showSuccess:msg];
            [weakSelf updateToken];
        } failure:^(NSString *errormsg) {
            [SCTool showError:errormsg];
        }];
    }
    
}

- (void)codeClick{
    
    SCScanController *scanVC = [[SCScanController alloc] init];
    
    [self.navigationController pushViewController:scanVC animated:YES];
}


- (void)updateToken{
    
    [SCApiTool ScRefreshSuccess:^(NSArray *data) {
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [UIApplication sharedApplication].keyWindow.rootViewController =[[JTNavigationController alloc] initWithRootViewController:[[HomeController alloc] init]];
        });

    } failure:^(NSString *errormsg) {
        
    }];
    
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end
